from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

from app.exchange import exchange_proxy, check_code


def wps_invoke(country_code, phone_num, phone_num_url, exchange_code):
    proxy_info = exchange_proxy.proxy_method();
    options = webdriver.FirefoxOptions()
    options.add_argument(f'--proxy-server=http://{proxy_info[0]}:{proxy_info[1]}')

    driver = webdriver.Firefox(options=options)
    driver.get("https://vip.wps.cn/spa/2020/exchange_card/")
    driver.maximize_window()

    time.sleep(3)
    login_module = WebDriverWait(driver, 5).until(
        EC.element_to_be_clickable((By.ID, 'exchangeBtn'))
    )
    login_module.click()

    # 取消自动登陆
    auto_login = WebDriverWait(driver, 5).until(
        EC.element_to_be_clickable((By.CLASS_NAME, 'keepOnline_txt'))
    )
    driver.execute_script('arguments[0].click();', auto_login)

    # 同意
    time.sleep(1)
    agree_button = driver.find_element(By.CLASS_NAME, 'js_protocol_tip_block')
    driver.execute_script('arguments[0].click();', agree_button)

    # 手机登陆
    time.sleep(2)
    phone_login = WebDriverWait(driver, 5).until(
        EC.element_to_be_clickable((By.CLASS_NAME, 'icon_round_phone'))
    )
    phone_login.click()

    # 选择地区
    if country_code != '+86':
        time.sleep(2)
        select_tag = WebDriverWait(driver, 5).until(
            EC.element_to_be_clickable((By.CLASS_NAME, 'js_phone_code_show'))
        )
        select_tag.click()

        time.sleep(2)
        select_element = driver.find_elements(By.CLASS_NAME, 'area-code-container__block-content-item-value')
        if select_element:
            for item in select_element:
                if item.text == country_code:
                    driver.execute_script('arguments[0].click()', item)

    # 输入手机号
    time.sleep(2)
    phone_input = driver.find_element(By.ID, 'phone')
    phone_input.clear()
    phone_input.send_keys(phone_num)

    # 人机校验
    time.sleep(2)
    robot_element = WebDriverWait(driver, 5).until(
        EC.element_to_be_clickable((By.ID, 'rectMask'))
    )
    time.sleep(1)
    robot_element.click()

    # 点击发送验证码
    time.sleep(1)
    send_code = WebDriverWait(driver, 5).until(
        EC.element_to_be_clickable((By.ID, 'resendBtn'))
    )
    send_code.click()

    # 调用获取手机验证码接口
    time.sleep(3)
    verify_code = check_code.get_check_code(phone_num_url)
    time.sleep(2)
    input_code = driver.find_element(By.ID, 'code')
    input_code.send_keys(verify_code)

    # 点击登陆
    time.sleep(2)
    login_button = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, 'confirmCode')))
    login_button.click()

    # 输入兑换码
    time.sleep(2)
    input_redeem = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, 'password')))
    time.sleep(1)
    input_redeem.send_keys(exchange_code)

    # 点击兑换
    time.sleep(2)
    exchange_button = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, 'exchangeBtn')))
    exchange_button.click()
    time.sleep(2)

    time.sleep(2)
    driver.quit()

if __name__ == '__main__':
    wps_invoke('+86', '18633448636', '1', 'xxxx-1234-xxxx-5678')